Debian Codegraph @

www/src/pages/[...slug].astro

raw
1 ---
2 import Layout from '../layouts/Layout.astro';
3 import mockServerScript from '../assets/js/mock-server.js?raw';
4 import Sidebar from '../components/Sidebar.astro';
5 import TableOfContents from '../components/TableOfContents.astro';
6 import Breadcrumbs from '../components/Breadcrumbs.astro';
7 import PageHeader from '../components/PageHeader.astro';
8 import Pagination from '../components/Pagination.astro';
9 import FinderLink from '../components/FinderLink.astro';
10 import { getFolder, render, COLLECTIONS } from '../lib/content';
11 import { buildDocsSidebar } from '../lib/docs-sidebar';
12
13 export async function getStaticPaths() {
14 const paths = [];
15
16 for (const collection of COLLECTIONS) {
17 const isHome = collection === 'home';
18 const folder = await getFolder(collection);
19
20 paths.push({
21 params: { slug: isHome ? undefined : collection },
22 props: { folder, collection }
23 });
24
25 folder.allFiles.forEach((file) => {
26 paths.push({
27 params: { slug: isHome ? file.slug : `${collection}/${file.slug}` },
28 props: { file, folder, collection }
29 });
30 });
31
32 // Reference subfolders are listed inline on /reference, no separate pages
33 if (collection !== 'reference') {
34 folder.folders.forEach((subfolder) => {
35 paths.push({
36 params: { slug: `${collection}/${subfolder.slug}` },
37 props: { subfolder, folder, collection }
38 });
39 });
40 }
41 }
42
43 return paths;
44 }
45
46 const { file, subfolder, folder, collection } = Astro.props;
47
48 const isCollectionRootPage = !file && !subfolder;
49 const isSubfolderIndexPage = !!subfolder;
50 const isHomePage = isCollectionRootPage && collection === 'home';
51 const page = isCollectionRootPage ? folder! : isSubfolderIndexPage ? subfolder! : file;
52
53 const {Content} = await render(page);
54 const {title, description, keywords} = page.frontmatter;
55
56 // Display flags
57 const isReference = collection === 'reference';
58 const isReferenceIndex = isCollectionRootPage && isReference;
59 const isPatternsIndex = isCollectionRootPage && collection === 'patterns';
60 const isExtensionsIndex = isCollectionRootPage && collection === 'extensions';
61 const isEssaysIndex = isCollectionRootPage && collection === 'essays';
62 const isDocsRoot = isCollectionRootPage && collection === 'docs';
63 // Extensions is flat (no subfolders), but sibling navigation is useful, so show sidebar on inner pages.
64 const isFlatCollectionWithSidebar = collection === 'extensions';
65 const includeSidebar = isDocsRoot
66 || (!!(folder?.folders?.length || (isFlatCollectionWithSidebar && folder?.files?.length))
67 && !isReferenceIndex && !isPatternsIndex && !isExtensionsIndex);
68 const includeHeader = !isHomePage;
69 const includeMockServer = file?.frontmatter?.includeMockServer === true;
70 const includePagination = !isCollectionRootPage && !isSubfolderIndexPage && !!(file?.prev || file?.next);
71 const useWideLayout = (isCollectionRootPage || isSubfolderIndexPage) && collection === 'about';
72
73 let headings: any[] = [];
74 let sidebarFolder = folder!;
75 if (file) {
76 headings = (await render(file)).headings;
77 } else if (isReferenceIndex) {
78 headings = folder!.folders.map(sub => ({ depth: 2, slug: sub.slug, text: sub.frontmatter.title }));
79 } else if (isDocsRoot) {
80 ({headings, sidebar: sidebarFolder} = await buildDocsSidebar(folder));
81 headings = headings.map(h => h.depth === 2 ? {...h, label: true} : h);
82 }
83 const includeTableOfContents = headings.length > 0;
84 ---
85
86 <Layout title={title}
87 description={description}
88 keywords={keywords}
89 article={!isCollectionRootPage ? {
90 publishedTime: file?.frontmatter.created?.toISOString(),
91 modifiedTime: file?.frontmatter.modified?.toISOString(),
92 authors: file?.frontmatter.authors,
93 tags: file?.frontmatter.tags,
94 } : undefined}
95 breadcrumbs={page.breadcrumbs}
96 hasSidebar={includeSidebar}>
97
98 {includeSidebar && <Sidebar folder={sidebarFolder} />}
99
100 <div id="content-wrapper" class:list={[includeSidebar && "lg:pl-72"]}>
101 <main id="content-main-content" class="flex-1" data-home-page={isHomePage ? '' : undefined}>
102 <div class="w-full mx-auto px-4 sm:px-6 lg:px-8 flex justify-center max-w-360">
103
104 <div class:list={["pt-6 sm:pt-8 lg:pt-12 flex-1 w-full relative starting:opacity-0 transition", useWideLayout ? 'max-w-4xl' : 'max-w-prose']}>
105 <Breadcrumbs breadcrumbs={page.breadcrumbs} />
106
107 {includeHeader && <PageHeader {title} {description} file={file ?? (isDocsRoot ? folder : undefined)} {collection} />}
108
109 <article class:list={["prose prose-htmx prose-sm sm:prose-base dark:prose-invert max-w-none", (isDocsRoot || isReferenceIndex || isEssaysIndex || isPatternsIndex || isExtensionsIndex) && "sticky-headers"]}>
110 {includeMockServer && <script is:inline set:html={mockServerScript}></script>}
111 <Content/>
112 {includePagination && <Pagination prev={file!.prev} next={file!.next} />}
113 </article>
114 {isSubfolderIndexPage && (
115 <nav class="not-prose mt-4 flex flex-col gap-1">
116 {subfolder!.files.map((f) => (
117 <FinderLink href={f.url}>
118 <span class="flex-1 min-w-0 flex flex-col sm:flex-row sm:items-center gap-0.5 sm:gap-4">
119 <span class:list={["text-[13px] sm:text-sm leading-snug truncate", isReference && "font-mono"]} set:html={f.frontmatter.title} />
120 {f.frontmatter.description && (
121 <span class="text-[11px] text-neutral-500 dark:text-neutral-450 group-interact/link:text-blue-200 dark:group-interact/link:text-black truncate">
122 {f.frontmatter.description}
123 </span>
124 )}
125 </span>
126 </FinderLink>
127 ))}
128 </nav>
129 )}
130 </div>
131
132 {includeTableOfContents && (
133 <aside class:list={[
134 "hidden xl:flex xl:flex-col xl:w-60 xl:min-w-60 shrink-0 xl:ml-16 self-start sticky top-(--header-height) h-[calc(100vh-var(--header-height))] pt-27 pb-8 pr-4",
135 includeSidebar ? "xl:-me-16" : "xl:-me-[19rem]",
136 ]}>
137 <div class="shrink-0 mb-2">
138 <h2 class="text-[0.625rem] font-sans tracking-widest uppercase text-neutral-600 dark:text-neutral-400">
139 On this page
140 </h2>
141 </div>
142 <div class="flex-1 min-h-0 overflow-hidden">
143 <div class="h-full overflow-y-auto scrollbar-subtle">
144 <TableOfContents {headings}/>
145 </div>
146 </div>
147 </aside>
148 )}
149 </div>
150 </main>
151 </div>
152
153 </Layout>